<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style type="text/css">
        .router-link-exact-active{
            color:white;
            background-color: green;
        }
    </style>
</head>
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
<script src="https://unpkg.com/vue-router@3.5.1/dist/vue-router.js"></script>

<body>
    <div id="app">
        <keep-alive include="Home">
            <router-view></router-view>
        </keep-alive>
    </div>

    <template id="daohangTemp">
        <div style="display: flex;align-items: center;justify-content:space-between">
            <router-link to="/" >班级首页 </router-link>

            <router-link to="/about">班级详情</router-link>

            <router-link to="/person">个人详情</router-link>
        </div>
    </template>
    
    <!----定义一些组件-->
    <template id="HomeTemp">
            <div>
                <daohang></daohang>
                <div>
                    <span style="text-align: center;">班级首页</span>
                    <br>
                    <span>被点击了{{clickCounter}}次</span>
                    <br>
                    <button @click="clickBtnHandler"> 点击增加</button>
                </div>
            </div>
    </template>


    <template id="AboutTemp">
        <div>
            <daohang></daohang>
            <br>
            <div>
                <span>班级详情</span>
                <img src="./img/about.jpg">
            </div>
        </div>
    </template>


    <template id="PersonTemp">
        <div>
            <daohang></daohang>
            <br>
            <div>
                <span>个人主页</span>
                <br>
                <span>被点击了{{clickCounter}}次</span>
                <br>
                <button @click="clickBtnHandler"> 点击增加</button>
            </div>
        </div>
    </template>

</body>
<script>
    var daoHang=Vue.component('daohang', {template: '#daohangTemp'});
    var Home=Vue.component('Home', {
        template: '#HomeTemp',
        data:function(){
            return {
                clickCounter:0
            }
        },
        methods:{
            clickBtnHandler:function(){
                this.clickCounter++;
            }
        },
        created:function(){
            console.log("组件被创建了");
        },
        mounted:function(){
            console.log("组件被加载了");
            this.clickCounter=100;
        },
        updated:function(){
            console.log("组件被修改了");
        },
        destroyed:function(){
            console.log("组件被销毁了");
        }
    });
    var About=Vue.component('About', {template: '#AboutTemp'});
    var Person=Vue.component('Person', {
        template: '#PersonTemp',
        data:function(){
            return {
                clickCounter:0
            }
        },
        methods:{
            clickBtnHandler:function(){
                this.clickCounter++;
            }
        },
        created:function(){
            console.log("组件被创建了");
        },
        mounted:function(){
            console.log("组件被加载了");
            this.clickCounter=100;
        },
        updated:function(){
            console.log("组件被修改了");
        },
        destroyed:function(){
            console.log("组件被销毁了");
        }
    });

    // 创建 router 实例
    var router = new VueRouter({
      routes: [
        { path: '/', component: Home },
        { path: '/about', component: About },
        {path:"/person",component:Person}
      ]
    });

    // 创建和挂载根实例
    new Vue({
        el: '#app',
        router // 使用 router 插件
    })
</script>
</html>